package text.jdbc;

import com.mysql.jdbc.Driver;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/*
    作业1：使用jdbc对dept中部门编号为30的数据进行删除
	删除语句delete from 表名 where 判断条件;
 */
public class JdbcText01 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        //创建Properties对象
        Properties properties = new Properties();

        //读取properties 文件
        properties.load(new BufferedReader(new FileReader("first_git/src/main/resources/Jdbc.properties")));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String sql1 = "delete from dept where deptNo=30";
        String sql2 = "select * from dept ";

        //注册驱动
        Class.forName(properties.getProperty("Driver"));

        //创建连接
        Connection con = DriverManager.getConnection(url,user,password);

        //测试连接 System.out.println(con);

        Statement statement = con.createStatement();
        int i = statement.executeUpdate(sql1);
        if(i>0){
            System.out.println("修改成功，修改后的查询结果如下");
            ResultSet resultSet = statement.executeQuery(sql2);
            while (resultSet.next()){
                String deptNo = resultSet.getString("deptNo");
                String dname = resultSet.getString("dname");
                String loc = resultSet.getString("loc");
                System.out.println(String.format("%s\t%s\t%s",deptNo,dname,loc));
            }
        }else {
            System.out.println("没有找到符合条件的数据，修改失败");
        }
     statement.close();
     con.close();
    }
}
